import waterMeshTool from '../lib/waterMeshTool'
import { Notification } from 'element-ui'

export default {
    data() {
        return {
            id_YB_HS_Card: ''
        }
    },
    computed: {
        basin() {
            return this.$store.getters.basin
        },
        yubaoFenXi_YMFW() {
            return this.$store.getters.pageState.yubaoFenXi_YMFW
        }
    },
    watch: {
        $route: {
            handler: function(val, oldVal) {
                if (val.path !== '/prediction/ShortPro/index') {
                    this.clearYB_HS_YMFW()
                }
            },
            // 深度观察监听
            deep: true
        },
        yubaoFenXi_YMFW: {
            immediate: true,
            deep: true,
            handler: function(val, oldVal) {
                if (this.$route.path === '/prediction/ShortPro/index'||this.$route.path === '/Rehearsal') {
                    if (val) {
                        this.getMeshYB_HS_YMFW()
                    } else {
                        this.clearYB_HS_YMFW()
                    }
                }
            }
        }
    },
    destroyed() {
        this.clearYB_HS_YMFW()
    },
    created() {
        var colors05 = waterMeshTool.gradient('#00A2FB', '#F0C90B', 20)
        var colors51 = waterMeshTool.gradient('#F0C90B', '#F0810B', 20)
        var colors12 = waterMeshTool.gradient('#F0810B', '#CE1F1F', 20)
        this.colorAll = [...colors05, ...colors51, ...colors12]
        this.dataSourceYB_HS_YMFWFW = null
        this.$eventBus.$on('EB_YB_HS_Card_Click1', option => {
            this.id_YB_HS_Card = option.fnid
            if (!this.yubaoFenXi_YMFW) return
            this.getMeshYB_HS_YMFW()
        })
    },
    mounted() {
        this.$nextTick(() => {
            // this.getMeshYB_HS_YMFW('20230604100000SX00D002')
        })
    },
    methods: {
        getYB_HS_YMFWData() {
            return new Promise((resolve, reject) => {
                var getPoint = waterMeshTool.getPoints(this.basin)
                var getMesh = waterMeshTool.getMeshs(this.basin)
                var getApi = this.$api.Get('/api/FloodAnalysis/GetFloodList', {
                    ID: this.id_YB_HS_Card
                })
                Promise.all([getPoint, getMesh, getApi]).then(([resPoint, resMesh, resApi]) => {
                    if (resApi.data.length < 1) {
                        resolve([])
                        return
                    }
                    var colorIndex = 3
                    var meshToSave = []
                    resApi.data.forEach(item => {
                        if (item.h > 2) {
                            colorIndex = 59
                        } else if (item.h > 1) {
                            colorIndex = Math.round(item.h * 20 - 20) + 39
                        } else if (item.h > 0.5) {
                            colorIndex = Math.round((item.h - 0.5) * 0.4 * 100) + 20
                        } else {
                            colorIndex = Math.round(item.h * 0.4 * 100)
                        }
                        const pointIndex = resMesh.pointIndexs[Number(item.guid)]

                        if (pointIndex && pointIndex.length > 0) {
                            const lonlats = []
                            pointIndex.forEach(jtem => {
                                const lonlat = resPoint.lonlats[Number(jtem)]
                                lonlats.push(...lonlat)
                            })
                            const cartesian3 = Cesium.Cartesian3.fromDegreesArray(lonlats)
                            meshToSave.push({ guid: item.guid, tm: item.tm, h: item.h, cartesian3, colorIndex })
                        } else {
                            // console.log(item.guid)
                        }
                    })
                    resolve(meshToSave)
                })
            })
        },
        getMeshYB_HS_YMFW() {
            this.clearYB_HS_YMFW()
            this.$store.commit('mapLoadingPush', '请求淹没数据...')
            this.getYB_HS_YMFWData().then(meshs => {
                if (meshs.length < 1) {
                    Notification({
                        message: `此模拟无淹没范围`,
                        type: 'warning'
                    })
                    this.$store.commit('mapLoadingDel', '请求淹没数据...')
                    return
                }
                this.dataSourceYB_HS_YMFWFW = new Cesium.CustomDataSource('YB_HS_YMFW_DataSource')
                meshs.forEach(item => {
                    // console.log(item)
                    const color = Cesium.Color.fromCssColorString(this.colorAll[item.colorIndex])
                    var polyEntity = {
                        eventName: '最大淹没范围',
                        prop: item,
                        polygon: {
                            hierarchy: item.cartesian3,
                            // perPositionHeight: true,
                            material: color.withAlpha(0.9)
                        }
                    }
                    this.dataSourceYB_HS_YMFWFW.entities.add(polyEntity)
                })
                this.$viewer.dataSources.add(this.dataSourceYB_HS_YMFWFW).then(res => {
                    this.$store.commit('mapLoadingDel', '请求淹没数据...')
                })

                // this.initEventYB_HS_YMFW() // ID无效
            })
        },
        initEventYB_HS_YMFW() {
            var eventFn = pickedObject => {
                if (
                    pickedObject &&
                    pickedObject.id &&
                    pickedObject.id.eventName
                ) {
                    if (pickedObject.id.eventName === '最大淹没范围') {
                        const dlgParams = this.$store.getters.dlgParams
                        dlgParams.name = 'floodDlog'
                        dlgParams.params = { ID: this.id_YB_HS_Card, Guid: pickedObject.id.prop.guid }
                        this.$store.dispatch('dlgParams', dlgParams)
                    }
                }
            }
            this.$store.getters.eventCesiumOption.leftPickFns.set('最大淹没范围', eventFn)
        },
        clearYB_HS_YMFW() {
            if (this.dataSourceYB_HS_YMFWFW) {
                this.$viewer.dataSources.remove(this.dataSourceYB_HS_YMFWFW)
                this.dataSourceYB_HS_YMFWFW = null
            }
        }
    }
}
